package com.crishne.anwo.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crishne.anwo.constant.JsonStringConstant;
import com.crishne.anwo.dto.Pager;
import com.crishne.anwo.model.Dic;
import com.crishne.anwo.service.DicService;
import com.crishne.anwo.utill.CommonUtill;

@Controller
@RequestMapping("sysmgr/dic/")
public class DicController {
	
	@Autowired
	private DicService dicService;
	
	private CommonUtill commonUtill = new CommonUtill();


	//返回jsp视图
	@RequestMapping(value = "dic_main")
	public String dic_main(){
		return "admin/sysmgr/dic_main";
	}
	
	@RequestMapping("getDicPageList")
	public void getDicPageList(HttpServletResponse response,HttpServletRequest request){
		//获取当前页 和 一页显示的数据数目
		String conCurrentPage = request.getParameter("currentpage");
		String stPageSize = request.getParameter("pageSize");
		String stDicName = request.getParameter("dicName");
		Integer currentpage = 1;
		Integer pageSize = 10;
		if(conCurrentPage != null && !"".equals(conCurrentPage)){
			currentpage = Integer.valueOf(conCurrentPage);
		}
		
		if(stPageSize != null && !"".equals(stPageSize)){
			pageSize = Integer.valueOf(stPageSize);
		}
		
		
		Dic searchDic = new Dic();
		searchDic.setStDicNamespaceText(stDicName);
		
		Pager dicPageList = dicService.findPageList(searchDic, currentpage, pageSize);
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(dicPageList);

		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("getDicTopList")
	public void getDicTopList(HttpServletResponse response,HttpServletRequest request){
		
		List<Map<String,Object>> dicTopMap = dicService.findMapTopName();
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(dicTopMap);

		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("getDicEdit")
	public void getDicEdit(HttpServletResponse response,HttpServletRequest request)
	{
		String dicId = request.getParameter("dicid");
		Dic tDic = dicService.getById(dicId);
		
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(tDic);
		commonUtill.OutputJson(jsonString,response,request);
	}
    
    @RequestMapping("saveOrEditDic")
	public void saveOrEditDic(HttpServletResponse response,HttpServletRequest request){
		//获取参数
    	String stDicId = request.getParameter("stDicId");
		String stDicName = request.getParameter("stDicName");
		String stDicCode = request.getParameter("stDicCode");
		String stDicNamespaceText = request.getParameter("stDicNamespaceText");
		String stDicNamespaceValue = request.getParameter("stDicNamespaceValue");
		String nmOrder = request.getParameter("nmOrder");
		
		Dic tDic = null;
		if(null == stDicId || "".equals(stDicId)){
			tDic = new Dic();
		}
		else{
			tDic = dicService.getById(stDicId);
		}
		
		tDic.setStDicName(stDicName);
		tDic.setStDicCode(stDicCode);
		tDic.setStDicNamespaceText(stDicNamespaceText);
		tDic.setStDicNamespaceValue(stDicNamespaceValue);
		if(null != nmOrder && !"".equals(nmOrder)){
			tDic.setNmOrder(Integer.valueOf(nmOrder));
		}
		else{
			tDic.setNmOrder(0);
		}
		
		
		if(null == stDicId || "".equals(stDicId)){
			tDic.setStDicId(UUID.randomUUID().toString());
			dicService.save(tDic);
		}
		else{
			dicService.update(tDic);
		}
		
		JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("保存成功！");

		commonUtill.OutputJson(jsonString, response, request);
    }
    
    @RequestMapping("deleteDic")
    public void deleteDic(HttpServletResponse response,HttpServletRequest request){
    	String dicIds = request.getParameter("dicId");
    	JSONObject jsonString = new JSONObject();
    	try{
    		if(null != dicIds && !"".equals(dicIds)){
    			String[] ids = dicIds.split(",");
    			for(int i=0 ;i<ids.length;i++){
    				dicService.delete(ids[i]);
    			}
        		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除成功！");
    		}
    		else{
    			jsonString = JsonStringConstant.getNoDataSuccessMessage("删除失败！");
    		}
    	}
    	catch(Exception ex){
    		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除失败！");
    	}
    	
		commonUtill.OutputJson(jsonString, response, request);
    }
    
    @RequestMapping("getDicsByNameSpaceValue")
	public void getDicsByNameSpaceValue(HttpServletResponse response,HttpServletRequest request){
    	String nameSpaceValue = request.getParameter("nameSpaceValue");
    	Dic searchDic = new Dic();
    	searchDic.setStDicNamespaceValue(nameSpaceValue);
		List<Dic> dicList = dicService.findList(searchDic);
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(dicList);

		commonUtill.OutputJson(jsonString, response, request);
    }

}